﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppEventLib
{
    internal static class StorageHelper
    {
        private static object lockObject = new object();

        private static volatile bool PrivateLocalStoragePathCreated = false;

        private static volatile bool PrivateStoragePathComputed = false;

        private static string PrivateStoragePath = "";

        internal static string LocalStoragePath()
        {
            if (!StorageHelper.PrivateLocalStoragePathCreated)
            {
                object obj = StorageHelper.lockObject;
                lock (obj)
                {
                    if (!StorageHelper.PrivateLocalStoragePathCreated)
                    {
                        if (!StorageHelper.FolderExists("AppEvents"))
                        {
                            StorageHelper.CreateFolder("AppEvents");
                        }
                        StorageHelper.PrivateLocalStoragePathCreated = true;
                    }
                }
            }
            return "AppEvents";
        }

        private static string StoragePath()
        {
            if (!StorageHelper.PrivateStoragePathComputed)
            {
                object obj = StorageHelper.lockObject;
                lock (obj)
                {
                    bool arg_20_0 = StorageHelper.PrivateStoragePathComputed;
                }
            }
            return StorageHelper.PrivateStoragePath;
        }

        internal static ulong AvailableFreeSpace()
        {
            try
            {
                var drives = DriveInfo.GetDrives();
                foreach (var item in drives)
                {
                    if (Application.StartupPath.StartsWith(item.Name))
                    {
                        return (ulong)item.AvailableFreeSpace;
                    }
                }
            }
            catch { }
            return 0uL;
        }

        internal static void CreateFolder(string path)
        {
            StorageHelper.GetStore().CreateDirectory(path);
        }

        internal static object Load(Type dataType)
        {
            object result = null;
            try
            {
                result = StorageHelper.Load(Path.Combine(StorageHelper.LocalStoragePath(), dataType.Name + ".js"), dataType);
            }
            catch (Exception arg_25_0)
            {
                //Sdk.LogInternalException(arg_25_0);
            }
            return result;
        }

        internal static object Load(string path, Type dataType)
        {
            object result = null;
            try
            {
                if (StorageHelper.FileExists(path))
                {
                    string text = StorageHelper.LoadString(path);
                    if (text != null)
                    {
                        try
                        {
                            result = JsonConvert.DeserializeObject(text, dataType);
                        }
                        catch (Exception)
                        {
                            try
                            {
                                StorageHelper.DeleteFile(path);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception arg_2F_0)
            {
                //Sdk.LogInternalException(arg_2F_0);
            }
            return result;
        }

        private static string LoadString(string path)
        {
            string result = null;
            try
            {
                using (IsolatedStorageFileStream isolatedStorageFileStream = StorageHelper.GetStore().OpenFile(path, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    result = new StreamReader(isolatedStorageFileStream).ReadToEnd();
                }
            }
            catch (Exception)
            {
            }
            return result;
        }

        internal static bool Save(object data)
        {
            bool result = false;
            try
            {
                string path = Path.Combine(StorageHelper.LocalStoragePath(), data.GetType().Name + ".js");
                result = StorageHelper.Save(data, path);
            }
            catch (Exception arg_2C_0)
            {
                //Sdk.LogInternalException(arg_2C_0);
            }
            return result;
        }

        internal static bool Save(object data, string path)
        {
            bool result = false;
            try
            {
                string dataString = JsonConvert.SerializeObject(data);
                StorageHelper.SaveString(path, dataString);
                result = true;
            }
            catch (Exception arg_14_0)
            {
                //Sdk.LogInternalException(arg_14_0);
            }
            return result;
        }

        private static void SaveString(string path, string dataString)
        {
            IsolatedStorageFile store = StorageHelper.GetStore();
            using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, store))
            {
                StreamWriter expr_17 = new StreamWriter(isolatedStorageFileStream);
                expr_17.Write(dataString);
                expr_17.Flush();
                expr_17.Close();
            }
        }

        internal static void DeleteFile(string path)
        {
            StorageHelper.GetStore().DeleteFile(path);
        }

        internal static bool FileExists(string path)
        {
            return StorageHelper.GetStore().FileExists(path);
        }

        internal static bool FolderExists(string path)
        {
            return StorageHelper.GetStore().DirectoryExists(path);
        }

        private static IsolatedStorageFile GetStore()
        {
            return IsolatedStorageFile.GetUserStoreForAssembly();
        }

        internal static DateTimeOffset GetCreationTime(string path)
        {
            return StorageHelper.GetStore().GetCreationTime(path);
        }

        internal static string[] GetFileNames(string path)
        {
            return StorageHelper.GetStore().GetFileNames(path + "\\*");
        }
    }
}
